<?php
require_once(DIR_LIBRARY."orm".DS."orm.php");
class Admin_Acl_Access_Index_Model extends NICED_Model
{

    public function getList($page, $page_size, $params = array())
    {
        $orm      = ORMFactory::get("acl.acl_access");
        $count    = 0;
        $max_page = 0;
        $rows     = $orm->getList($page, $page_size, $count, $max_page, $params);

        $this->set("count", $count);
        $this->set("max_page", $max_page);
        $this->set("page", $page);
        $this->set("accesses", $rows);
    }

    public function getById($id)
    {
        if (!$id)
        {
            throw new NICED_Exception("please assign id");
        }

        $orm = ORMFactory::get("acl.acl_access");
        $orm->byId($id);
        $orm->load();
        
        if (!$orm->getLoaded())
        {
            throw new NICED_Exception("get access fail");
        }

        $this->set("access", $orm);
    }

    public function create($name, $parent_id, $type_id, $link)
    {
        $orm = ORMFactory::get("acl.acl_access");
        $orm->setName($name);
        $orm->setLink($link);
        $orm->setField("parent_id", $parent_id);
        $orm->setField("type_id", $type_id);
        $this->set("access", $orm);

        $bool = $orm->create();
        
        if (!$bool)
        {
            throw new NICED_Exception("create fail");
        }
        
        return $bool;
    }

    public function update($id, $name, $parent_id, $link)
    {
        $orm = ORMFactory::get("acl.acl_access");
        $orm->byId($id);
        $orm->setName($name);
        $orm->setLink($link);
        $orm->setField("parent_id", $parent_id);
        $this->set("access", $orm);

        $bool = $orm->update($id);

        if (!$bool)
        {
            throw new NICED_Exception("update fail".$orm->buildSQL());
        }

        return $bool;
    }

    public function delete($id)
    {
        if (!$id)
        {
            throw new NICED_Exception("please assign id");
        }

        require_once(DIR_LIBRARY."orm".DS."orm.php");
        $orm = ORMFactory::get("acl.acl_access");
        $orm->byId($id);
        $bool = $orm->delete();
        
        if (!$bool)
        {
            throw new NICED_Exception("delete fail");
        }
        return $bool;
    }

}//end class
